/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controler;

import graph.ConditionsProperties;
import graph.Edge;
import graph.Node;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import view.EdgeItem;
import view.EdgePanel;
import view.SimulationWindow;

/**
 *
 * @author swider
 */
public class EdgePanelControler {
    private EdgePanel panel;
    private Controler controler;
    private EdgeItemControler itemcontroler;
    

    public EdgePanelControler(){
        panel = new EdgePanel();
        initActions();
    }

    public EdgePanelControler(Controler c){
        controler = c;
        panel = new EdgePanel();
        initActions();
    }
    public EdgePanel getView(){
        return panel;
    }
    public void initActions(){
        panel.addSave(new Save());
        panel.addDelete(new Delete());
        panel.addWeatherComboBoxAction(new SelectWeather());
        panel.addRoadComboBoxAction(new SelectRoad());
        panel.addRoadSliderAction(new SliderRoad());
        panel.addWeatherSliderAction(new SliderWeather());
    }

    public void setEdgeControler(EdgeItemControler edgeItem){
        Edge edge = edgeItem.getModel();
       
        panel.setEdgeName(edge.getName());
        panel.setEdgeVMax(edge.getVMax());
        panel.setEdgeLength(edge.getLength());
        panel.setEdgeLock(edge.isLock());
        panel.setWeather(edge.getWeatherConditions());
        panel.setTraffic(edge.getTraffic());
        panel.setRoad(edge.getRoadConditions());
        panel.setWeatherSlider(edge.getWeatherSlider());
        panel.setRoadSlider(edge.getRoadSlider());
        panel.setRoadSliderValue();
        panel.setWeatherSliderValue();
        panel.setRealSpeed(getSpeed(edge)+" ");
        panel.setRealTime(edge.getLength()/getSpeed(edge)*60+"");
        itemcontroler=edgeItem;

        if(edge.getWeatherConditions()==0){
            panel.setWeatherSliderVisible(false);
        }else{
            panel.setWeatherSliderVisible(true);
        }

        if(edge.getRoadConditions()==0){
            panel.setRoadSliderVisible(false);
        }else{
            panel.setRoadSliderVisible(true);
        }
    }
       public EdgeItemControler getItemControler(){
        return itemcontroler;
    }

     private void saveEdge() {
           controler.saveEdgeName(panel.getEdgeName(), this);
           controler.saveEdgeVMax(panel.getEdgeVMax(),this);
           controler.saveEdgeLength(panel.getEdgeLenght(),this);
           controler.saveEdgeLock(panel.getEdgeLock(),this);
           controler.saveEdgeWeather(panel.getWeather(),this);
           controler.saveEdgeRoad(panel.getRoad(),this);
           controler.saveEdgeTraffic(panel.getTraffic(),this);
           controler.saveEdgeWeatherSlider(panel.getWeatherSlider(),this);
           controler.saveEdgeRoadSlider(panel.getRoadSlider(),this);
           Edge edge = itemcontroler.getModel();
           panel.setRealSpeed(getSpeed(edge)+" ");
           panel.setRealTime(edge.getLength()/getSpeed(edge)*60+"");
           controler.getMainWindow().repaint();
        }

    private class Behavior implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            System.out.println("Przycisk Zachowanie w Edge");
        }
    }
       
    private class Save implements ActionListener{

         public void actionPerformed(ActionEvent e) {
            saveEdge();
        }

    ////////////////////ActionListenery

}
    private class Delete implements ActionListener{
        public void actionPerformed(ActionEvent e){
            controler.deleteEdge();
        }
    }

    private class SelectWeather implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            int index = panel.getSelectedWeatherIndex();
            if(index==0){
                panel.setWeatherSliderVisible(false);
            }else{
                panel.setWeatherSliderVisible(true);
            }
        }

    }

    private class SelectRoad implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            int index = panel.getSelectedRoadIndex();
            if(index == 0){
                panel.setRoadSliderVisible(false);
            }else{
                panel.setRoadSliderVisible(true);
            }
        }

    }

    private class SliderRoad implements ChangeListener{


        public void stateChanged(ChangeEvent e) {
            panel.setRoadSliderValue();
        }

    }

    private class SliderWeather implements ChangeListener{

        public void stateChanged(ChangeEvent e) {
             panel.setWeatherSliderValue();
        }



    }

    public double getSpeed(Edge e){

     double speed = e.getVMax();

        int selectedRoad = e.getRoadConditions();
        if(selectedRoad!=0){
            int roadValue = e.getRoadSlider();
            System.err.println(selectedRoad);
            double roadPercent = ((double)roadValue)/100;
            switch(selectedRoad){
                case 1: speed-= speed*(ConditionsProperties.REMONT*roadPercent/100);
                        break;
                case 2: speed-= speed*(ConditionsProperties.WYPADEK*roadPercent/100);
                        break;
                case 3: speed-= speed*(ConditionsProperties.UTRUDNIENIA_W_RUCHU*roadPercent/100);
                        break;
            }
        }


        int selectedWeather = e.getWeatherConditions();
        System.err.println("wybrana pogoda" + selectedWeather);
        if(selectedWeather!=0){
            int weatherValue = e.getWeatherSlider();
            System.err.println(selectedWeather);
            double weatherPercent = ((double)weatherValue)/100;
            System.out.println("pogoda" + weatherPercent);
            switch(selectedWeather){
                case 1: speed -= speed*(ConditionsProperties.DESZCZ*weatherPercent/100);
                        break;
                case 2: speed -= speed*(ConditionsProperties.SNIEG*weatherPercent/100);
                        break;
                case 3: speed -= speed*(ConditionsProperties.GRAD*weatherPercent/100);
                        break;
                case 4: speed -= speed*(ConditionsProperties.GOLOLEDZ*weatherPercent/100);
                        break;
                case 5: speed -= speed*(ConditionsProperties.MGLA*weatherPercent/100);
                        break;
            }
         }



        double trafficPercent = 0;
        System.err.println(e.getTraffic());
        if(e.getTraffic()!=0){
        switch(e.getTraffic()){
            case 0: trafficPercent =ConditionsProperties.SMALL/100;
                    break;
            case 1: trafficPercent = ConditionsProperties.MIDDLE/100;
                    break;
            case 2: trafficPercent = ConditionsProperties.BIG/100;
                    break;
        }
            speed=speed - speed*trafficPercent;
        }
        return speed;
    }

}
